package com.whnk.web.controller.pm;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.whnk.pm.util.ValidateException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whnk.common.annotation.Log;
import com.whnk.common.base.AjaxResult;
import com.whnk.common.config.Global;
import com.whnk.common.enums.BusinessType;
import com.whnk.common.support.Convert;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.pm.domain.BaseInfo;
import com.whnk.pm.service.IBaseInfoService;
import com.whnk.pm.service.IResumeService;

/**
 * 人员基本 信息操作处理
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Controller
@RequestMapping("/pm/baseInfo")
public class BaseInfoController extends BaseController
{
    private String prefix = "pm/baseInfo";
	
	@Autowired
	private IBaseInfoService baseInfoService;

	@Autowired
	private IResumeService resumeService;
	
	@RequiresPermissions("pm:baseInfo:view")
	@GetMapping()
	public String baseInfo()
	{
	    return prefix + "/baseInfo";
	}
	
	/**
	 * 查询人员基本列表
	 */
	@RequiresPermissions("pm:baseInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(BaseInfo baseInfo)
	{
		startPage();
		String name = baseInfo.getEmpNm();
		if (StringUtils.isNotBlank(name)) {
			baseInfo.setEmpNames(name.split(";"));
		}
        List<BaseInfo> list = baseInfoService.selectBaseInfoList(baseInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增人员基本
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存人员基本
	 */
	@RequiresPermissions("pm:baseInfo:add")
	@Log(title = "人员基本", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(BaseInfo baseInfo)
	{		
		return toAjax(baseInfoService.insertBaseInfo(baseInfo));
	}

	/**
	 * 修改人员基本
	 */
	@GetMapping("/edit/{empId}")
	public String edit(@PathVariable("empId") String empId, ModelMap mmap)
	{
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存人员基本
	 */
	@RequiresPermissions("pm:baseInfo:edit")
	@Log(title = "人员基本", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(BaseInfo baseInfo)
	{		
		return toAjax(baseInfoService.updateBaseInfo(baseInfo));
	}
	
	/**
	 * 删除人员基本
	 */
	@RequiresPermissions("pm:baseInfo:remove")
	@Log(title = "人员基本", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(baseInfoService.deleteBaseInfoByIds(ids));
	}
	
	/**
	 * 查看详细信息
	 * 
	 * @param empId
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("pm:baseInfo:detail")
    @GetMapping("/detail/{empId}")
    public String detail(@PathVariable("empId") String empId, ModelMap mmap)
    {
        BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
        return prefix + "/detail";
    }
	
	/**
	 * 更新教育经历
	 * @param empId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/upEdu/{empId}")
    public String upEdu(@PathVariable("empId") String empId, ModelMap mmap)
    {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
        return "pm/eduInfo/eduInfo";
    }
	
	
	/**
	 * 更新工作经历
	 * @param empId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/upWork/{empId}")
    public String upWork(@PathVariable("empId") String empId, ModelMap mmap)
    {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
        return "pm/workInfo/workInfo";
    }
	
	/**
	 * 更新项目经历
	 * @param empId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/upProj/{empId}")
    public String upProj(@PathVariable("empId") String empId, ModelMap mmap)
    {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
        return "pm/projInfo/projInfo";
    }
	
	/**
	 * 更新附件信息
	 * @param empId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/upFile/{empId}")
    public String upFile(@PathVariable("empId") String empId, ModelMap mmap)
    {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
        return "pm/fileInfo/fileInfo";
    }
	
	/**
     * TODO 批量生成简历
     */
    @PostMapping("/expResuByEmpIds")
    @ResponseBody
    public AjaxResult expResuByEmpIds(String ids) throws UnsupportedEncodingException {
		String filename;
		List<String> errorMsg = new ArrayList<>();
		try {
			filename = resumeService.downloadResume(Arrays.asList(Convert.toStrArray(ids)), errorMsg);
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof ValidateException) {
				if (errorMsg.size() == 0) {
					return success();
				} else {
					StringBuffer buffer = new StringBuffer();
					for (String error : errorMsg) {
						buffer.append(error).append("<br/>");
					}
					return error(buffer.toString());
				}
			}
			return AjaxResult.error("下载失败");
		}
		String result = URLEncoder.encode(filename, "UTF-8");
		return AjaxResult.success(result);
    }
    
    
    @PostMapping("/template")
    @ResponseBody
    public AjaxResult downloadTemplate(){
    	  try {
			InputStream stream =BaseInfoController.class.getResourceAsStream("/template.xlsx");
			File downloadFile = new File(Global.getDownloadPath() + File.separator + "template.xlsx");
			
			FileUtils.copyToFile(stream, downloadFile);

			String filename = downloadFile.getName();
			String result = URLEncoder.encode(filename, "UTF-8");
			return AjaxResult.success(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	 return AjaxResult.error("下载失败");
    }

    
	/**
     * 校验证件号码
     */
    @PostMapping("/checkEmpIdUnique")
    @ResponseBody
    public String checkEmpIdUnique(BaseInfo baseInfo)
    {
        return baseInfoService.checkEmpIdUnique(baseInfo.getEmpId());
    }
    
    /**
     * 校验证件号码是否存在
     */
    @PostMapping("/checkEmpIdExist")
    @ResponseBody
    public String checkEmpIdExist(BaseInfo baseInfo)
    {
        return baseInfoService.checkEmpIdExist(baseInfo.getEmpId());
    }

	/**
	 *
	 */
	@RequiresPermissions("admin:verify")
	@PostMapping("/verify")
	@ResponseBody
	public AjaxResult verify(String id, String status)
	{
		BaseInfo baseInfo = new BaseInfo();
		baseInfo.setEmpId(id);
		baseInfo.setEmpVerifyStatus(status);
		baseInfoService.updateBaseInfo(baseInfo);
		return success();
	}
    
}
